#include "snake.h"
#include "ui_snake.h"
#include<QPainter>
#include<QTimer>
#include<QKeyEvent>
Snake::Snake(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Snake),blsRun(false),Speed(500)
{
    ui->setupUi(this);
    this->setGeometry(QRect(600,300,290,310));
}

Snake::~Snake()
{
    delete ui;
}
void Snake::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    if(!blsRun)
    InitSnake();
    //画游戏背景
    //外墙
    painter.setPen(Qt::BlankCursor);//外边框的画笔颜色
    painter.setBrush(Qt::gray);//设置填充颜色
    painter.drawRect(15,15,260,260);//15,15距离窗体的单位
    //内墙
    painter.setPen(Qt::BlankCursor);//内边框的画笔颜色
    painter.setBrush(Qt::black);//设置填充颜色
    painter.drawRect(20,20,250,250);//15,15距离窗体的单位

    //画格子
    painter.setPen(Qt::blue);
    for(int i=2;i<=27;i++)
    {
        painter.drawLine(20,i*10,270,i*10);//起始点坐标（前两个起始点横纵坐标，后两个终点横纵坐标）
        painter.drawLine(i*10,20,i*10,270);//画竖线
    }
    //显示游戏开始和结束
     QFont font1("Courier",24);
     painter.setFont(font1);
     painter.setPen(Qt::red);
     painter.setBrush(Qt::red);
     painter.drawText(40,150,Display);//字体显示位置

     //显示分数
     QFont font2("Courier",15);//字体及字体颜色
     painter.setFont(font2);
     painter.setPen(Qt::blue);
     painter.setBrush(Qt::blue);
     painter.drawText(140,300,"得分：");
     painter.drawText(230,300,QString::number(Score));//字体显示位置
    //画🐍
    painter.setPen(Qt::black);
    painter.setBrush(Qt::darkGreen);
    //painter.drawRect(vSnakeRect);
    painter.drawRect(Food);
    painter.drawRects(&vSnakeRect[0],vSnakeRect.size());
    if(blsOver)
        timer->stop();


}
void Snake::InitSnake(){
    blsRun=true;
    blsOver=false;
    nDirection=2;//定义🐍初始时向哪个方向行走
    Display="游戏开始！";
    Score=0;
    Food=CreateFood();
    //QRect rect(100,70,10,10);
   // vSnakeRect=rect;//每一个小方块
    vSnakeRect.resize(5);//蛇的长度是5
    for(int i=0;i<vSnakeRect.size();i++){
        QRect rect(100,70+10*i,10,10);
        vSnakeRect[vSnakeRect.size()-1-i]=rect;//设置蛇的坐标

    }

  timer=new QTimer(this);//设定计时器
  timer->start(500);//设定时间500ms//信号和槽,时间就是信号
  connect(timer,SIGNAL(timeout()),SLOT(Snake_update()));

}
void Snake::Snake_update(){
    Display="";
    SnakeHead=vSnakeRect.first();//获取蛇头
    IsEat();
    IsHit();
    for(int j=0;j<vSnakeRect.size()-1;j++){
        vSnakeRect[vSnakeRect.size()-1-j]=vSnakeRect[vSnakeRect.size()-2-j];
    }
    switch(nDirection)//ndirection表示按下的按件
    {
      case 1:
        SnakeHead.setTop(SnakeHead.top()-10);
        SnakeHead.setBottom(SnakeHead.bottom()-10);//向上
        break;
      case 2:
        SnakeHead.setTop(SnakeHead.top()+10);
        SnakeHead.setBottom(SnakeHead.bottom()+10);//向下
        break;
      case 3:
        SnakeHead.setLeft(SnakeHead.left()-10);
        SnakeHead.setRight(SnakeHead.right()-10);//向坐
        break;
       case 4:
        SnakeHead.setLeft(SnakeHead.left()+10);
        SnakeHead.setRight(SnakeHead.right()+10);
        break;//向右
       default:;
    }//按下后移动格数
    vSnakeRect[0]=SnakeHead;
    if(SnakeHead.left()<20||SnakeHead.right()>270||SnakeHead.top()<20||SnakeHead.bottom()>270)
    {   blsOver=true;
        Display="游戏结束！";}

    update();//更新

}
void Snake::keyPressEvent(QKeyEvent *event){
    QKeyEvent *key=(QKeyEvent*)event;
    switch(key->key()){
       case Qt::Key_Up:nDirection=1;
            break;
       case Qt::Key_Down:nDirection=2;
            break;
       case Qt::Key_Left:nDirection=3;
            break;
       case Qt::Key_Right:nDirection=4;
            break;
       default:;
    }//控制按键
}
QRect Snake::CreateFood(){
    int x,y;
    x=qrand()%25;
    y=qrand()%25;
    QRect rect(20+x*10,20+y*10,10,10);
    return rect;
}
void Snake::IsEat(){
    if(SnakeHead==Food){
       SnakeHead=Food;
       vSnakeRect.push_back(vSnakeRect.last());
       Food=CreateFood();
       Score=Score+10;
       Speed=Speed-20;//吃到食物后减少的速度；
       timer->stop();//停止当前的时间
       timer->start(Speed);//时间从新的速度开始
    }
}
void Snake::IsHit(){
    for(int i=1;i<vSnakeRect.size();i++){
        if(SnakeHead==vSnakeRect[i]){
            Display="游戏结束！";
            blsOver=true;
            update();
        }
    }
}
